package com.ukao.steward.ui.menuLeft.myEarnings;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ukao.steward.R;
import com.ukao.steward.adapter.BankCardAdapter;
import com.ukao.steward.base.BaseFragment;
import com.ukao.steward.bean.BankCardListBena;
import com.ukao.steward.consts.Constant;
import com.ukao.steward.consts.SaveParams;
import com.ukao.steward.listener.OnItemClickListener;
import com.ukao.steward.retrofit.ApiCallback;
import com.ukao.steward.util.T;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 银行卡列表
 */
public class BankCardListFragment extends BaseFragment   {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.back_btn)
    ImageView backBtn;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.title_layout)
    RelativeLayout titleLayout;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.add_btn)
    TextView addBtn;
    Unbinder unbinder;


    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    List<BankCardListBena.ListBean> datas=new ArrayList<>();
    private BankCardAdapter mBankCardAdapter;


    boolean loadfinish=false;
    public static BankCardListFragment newInstance(String param1, String param2) {
        BankCardListFragment fragment = new BankCardListFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_bank_card, container, false);

        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        initListener();
    }

    @Override
    protected void initView() {

        initLinearRecyclerView(recyclerView);
         mBankCardAdapter=new BankCardAdapter(_mActivity,datas);
         recyclerView.setAdapter(mBankCardAdapter);

    }


    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        tradeWalletlist();
    }

    @Override
    protected void initListener() {
        super.initListener();
        mBankCardAdapter.setOnItemClickListener(mOnItemClickListener);
    }

    private OnItemClickListener  mOnItemClickListener=new OnItemClickListener() {
        @Override
        public void onItemClick(int position) {
            BankCardListBena.ListBean item = mBankCardAdapter.getItem(position);
            if (!item.isCheck()) {
                item.setCheck(!item.isCheck());
                List<BankCardListBena.ListBean> mData = mBankCardAdapter.getDataList();
                for (int j = 0; j < mData.size(); j++) {
                    if (position != j) {
                        mData.get(j).setCheck(false);

                    }
                }

                mBankCardAdapter.notifyDataSetChanged();
            }
            Bundle  mBundle=new Bundle();
            mBundle.putString(Constant.ARG_PARAM1,item.getAccountName());
            mBundle.putString(Constant.ARG_PARAM2,item.getId());
            setFragmentResult(RESULT_OK, mBundle);
            finish();

        }
    };

    @Override
    public void onFragmentResult(int requestCode, int resultCode, Bundle data) {
        super.onFragmentResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case Constant.REQUEST_CODE:
                    loadfinish=false;
                    tradeWalletlist();

                    break;
            }
        }
    }




    @OnClick({R.id.back_btn, R.id.add_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.add_btn:
                startForResult(FillBlankFragment.newInstance("", ""), Constant.REQUEST_CODE);


                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    public void tradeWalletlist( ) {
        if (loadfinish)return;
        showProgressDialog();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("accessToken", SaveParams.getToken());
        addSubscription(apiStores().tradeWalletlist(Constant.createParameter(mHashMap)),
                new ApiCallback<BankCardListBena>() {

                    @Override
                    public void onSuccess(BankCardListBena model) {

                        if (model.getHttpCode() == 200) {
                            loadfinish=true;
                            mBankCardAdapter.setDataList(model.getData().getList());

                        }else{

                            T.show(model.getMsg());
                        }

                    }
                    @Override
                    public void onFinish() {
                        dismissProgressDialog();


                    }
                });
    }
}
